package _2019_08_06_container;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Hello world!
 * 此代码想构建一个容器。一个线程添加，另一个线程监听，到五个的时候结束第二个线程
 */
public class MyContainer
{
    List list = new ArrayList();

    public void add(Object o) {
        list.add(o);
    }

    public int size() {
        return list.size();
    }

    public static void main(String[] args) {
        MyContainer container = new MyContainer();
        new Thread(() -> {
            System.out.println("Thread-1 start");
            for(int i=0; i<10; i++) {
                container.add(new Object());
                System.out.println("add " + i);

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            System.out.println("Thread-1 end");
        },"Thread-1 start").start();

        new Thread(() -> {
            System.out.println("Thread-2 start");
            while(true) {
                if(container.size() == 5) {
                    break;
                }
            }
            System.out.println("Thread-2 end");
        },"Thread-2").start();
    }

}
